package com.sheng.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sheng.entity.User;
import com.sheng.mapper.UserMapper;
import com.sheng.po.user.PageUserQuery;
import com.sheng.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheng.util.JwtUtils;
import com.sheng.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 用户管理 服务实现类
 * </p>
 *
 * @author 王胜
 * @since 2022-11-19 15:56
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserInfo(String token) {
        boolean hasKey = RedisUtils.hasKey(token);
        User user = new User();
        log.info("当前token是否存在？=>" +  hasKey);
        if (hasKey){
            String account = JwtUtils.getAccount(token);
            User userInfo = userMapper.getUserInfo(account);
            log.info(userInfo.toString());
            return userInfo;
        }
        return null;
    }

    @Override
    public PageInfo<User> getUserInfoByPage(PageUserQuery pageUserQuery) {
        PageHelper.startPage(pageUserQuery.getPageNumber(),pageUserQuery.getPageSize());
        return new PageInfo<User>(userMapper.getUserInfoByPage(pageUserQuery));
    }

    //新增用户
    @Override
    public Integer saveUser(User user) {
        return userMapper.saveUser(user);
    }

    //根据用户id获取用户信息
    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    //根据用户id删除信息
    @Override
    public Integer deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    //更新用户信息
    @Override
    public Integer updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }


}
